<template>
    <mi-form-page @submit="submit"
                  :loading="edit.loading"
                  title="会员信息编辑"
                  v-model:show="edit.show"
    >
        <el-form :model="edit.info"
                 ref="formRef"
                 :rules="rules">

            <el-form-item prop="avatar" label="头像">
                <up-avatar v-model="edit.info.avatar" />
            </el-form-item>

            <el-form-item prop="nickname" label="昵称">
                <edit-nickname v-model="edit.info.nickname" />
            </el-form-item>

            <el-form-item prop="mobile" label="手机号">
                <mi-input v-model="edit.info.mobile" />
            </el-form-item>

<!--            <el-form-item prop="level_id" label="等级">-->
<!--                <mi-input v-model="edit.info.level_id" />-->
<!--            </el-form-item>-->

<!--            <el-form-item prop="area" label="地址">-->
<!--                <mi-select-address :info="edit.info" />-->
<!--            </el-form-item>-->

<!--            <el-form-item prop="tag_ids" label="标签">-->
<!--                <mi-input v-model="edit.info.tag_ids" />-->
<!--            </el-form-item>-->

            <el-form-item prop="password" label="登录密码">
                <mi-input v-model="edit.info.password" type="password" show-password />
            </el-form-item>

            <el-form-item prop="parent_id" label="上级">
                <user-select v-model="edit.info.parent_id" />
            </el-form-item>


        </el-form>
    </mi-form-page>
</template>

<script setup lang="ts">
import {reactive, watch, ref, onBeforeMount, inject} from "vue";
import {makeModelAuth} from "@/utils/auth";
import List from "@/model/list";
import Edit from "@/model/edit";
import {FormInstance} from "element-plus";
import {simpleGet, simplePost} from "@/utils/request";
import UpAvatar from "@/views/site/user/components/UpAvatar.vue";
import EditNickname from "@/views/site/user/components/EditNickname.vue";
import UserSelect from "@/views/site/components/UserSelect.vue";

const formRef = ref<FormInstance>()
const auth = makeModelAuth("site.user")
const rules = {
    avatar:[{required:true, message:'必填'}],
    nickname:[{required:true, message:'必填'}],
    mobile:[{required:true, message:'必填'}],
}

const list = inject<List>("list")
const edit = inject<Edit>("edit", new Edit({}))

const submit = () => {
    let url = '/' + (edit.info.id ? auth.edit : auth.add)

    edit.submit({
        formRef: formRef.value,
        url: url,
        list: list
    })
}

watch(() => edit.show, (nv, ov) => {
    if (!nv) return;
    edit.init(async () => {
        const id= edit.info.id
        if (id){
            // 请求表单数据
            let res = await simpleGet('/' + auth.edit, {id:id})
            edit.info = res.info
        }else{
            edit.info = {
                is_show:1,
            }
        }
    })
})

</script>

<style scoped>

</style>
